/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// presmgr.hpp: PegResourceManager class definition
//
// Author: Swell Software
//
// Copyright 2006,2007 Swell Software, Inc.
//
// Notes: Any changes should be reflected in wbresmgr.hpp file.
//
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef _PEG_RES_MANAGER_
#define _PEG_RES_MANAGER_

#define LOCK_RESOURCE_MANAGER
#define UNLOCK_RESOURCE_MANAGER

/*--------------------------------------------------------------------------*/
#define LS(a)  PegResourceManager::LookupString(a)

/*--------------------------------------------------------------------------*/
typedef struct {
    PegFont   *pFont;
    PEGUINT FontId;
    PEGUBYTE Flags;
} PegFontTableEntry;

/*--------------------------------------------------------------------------*/
typedef struct {
    PegBitmap *pBitmap;
    PEGUINT BitmapId;
    PEGUBYTE  Flags;
} PegBitmapTableEntry;

/*--------------------------------------------------------------------------*/
typedef struct {
    PEGCOLOR Color;
    PEGUINT ColorId;
} PegColorTableEntry;


/*--------------------------------------------------------------------------*/
struct PegStringTablePage {
    PEGUINT FirstSID;
    PEGUINT LastSID;
    PEGUINT NumLanguages;
    const PEGCHAR   ***pTable;
    PegStringTablePage *pNext;
};

/*--------------------------------------------------------------------------*/
typedef struct {

    const PegStringTablePage  *pStringTable;
    const PegFontTableEntry   *pFontTable;
    const PegBitmapTableEntry *pBitmapTable;
    const PegColorTableEntry  *pColorTable;
    void                *pSpare1;
    void                *pSpare2;
} PegResourceTable;


/*--------------------------------------------------------------------------*/
#define NUM_SYSTEM_BITMAPS      MAX_BIDS
#define NUM_SYSTEM_COLORS       MAX_CIDS
#define NUM_SYSTEM_FONTS        MAX_FIDS

#define NUM_USER_FONTS          16
#define NUM_USER_BMPS           16
#define NUM_USER_COLORS         16

#define FIRST_USER_BMP          64
#define FIRST_USER_COLOR        NUM_SYSTEM_COLORS
#define FIRST_USER_FONT         32  // first user-defined font index

#define FIRST_USER_STRING       100

#define MAX_CACHED_GRADIENTS    32

#define INVALID_COLOR_ID        0xFFFFU

class PegResourceManager
{
    public:

        static void AddResource(const PegStringTablePage *pPage,
            PEGBOOL DeleteOld = FALSE);

        static void AddResource(PEGUINT BitmapId, PegBitmap *pMap,
                                PEGUINT Flags = 0, PEGBOOL DeleteOld = FALSE);

        static void AddResource(PEGUINT ColorId, PEGCOLOR Color);

        static void AddResource(PEGUINT FontId, PegFont *pFont,
                                PEGUINT Flags = 0, PEGBOOL DeleteOld = FALSE);

        #if defined(PEG_BIDI_SUPPORT)
         static PEGBOOL CharIsRTLPage(PEGCHAR);
        #endif

        static void DestroyBitmap(PEGUINT BitmapId);
        static void DestroyFont(PegFont *pFont);
        static void DestroyFont(PEGUINT FontId);
        static void destroyall(void);

        static PegBitmap *GetBitmap(PEGUINT BitmapId);

        static PEGUSHORT GetBitmapHeight(PEGUINT BitmapId);

        static PEGSHORT GetBitmapWidth(PEGUINT BitmapId);

        static void GetBitmapWidthHeight(PEGUINT BitmapId,
                                                PEGSHORT &Width,
                                                PEGSHORT &Height);

        static PEGCOLOR GetColor(PEGUINT ColorId);

        static PEGUINT GetCurrentLanguage(void) { return mCurrentLanguage; }
        static PEGCT   *GetLanguageName(PEGUINT Language);
        static PEGUINT GetNumLanguages(void);

        static PEGUINT GetFirstAvailableBitmapId(void);
        static PEGUINT GetFirstAvailableFontId(void);

        static PegFont *GetFont(PEGUINT FontId);

       #ifdef PEG_PRO
        static PegBitmap *GetGradient(const PegGradientStruct &Grad);
       #endif

        static void Initialize(void);

        static void InstallResourcesFromTable(PegResourceTable *pTable,
            PEGBOOL DeleteOld = FALSE);

        static const PEGCHAR *LookupString(PEGUINT Id);

       #if defined(PEG_UTF8_ENCODING)
        static PEGBOOL LookupUTFString(PEGUINT Id, PEGCHAR **PutStr);
       #endif

       #if defined(PEG_RUNTIME_RESOURCES)

        static PEGUSHORT GetResThemeCount(void);
        static PEGUSHORT GetResStringCount(void);
        static PEGBOOL LoadResourceFile(PegFile *pFile,
                PEGBOOL DelOldStrs = FALSE);
        static PegResourceTable *GetResourceTable(PEGINT Index);

       #endif

        static void SetCurrentLanguage(PEGUINT Language);
        static PEGBOOL SetLanguageName(PEGUINT Language, PEGCT *pName);
        static void    SetNumLanguages(PEGUINT Num);

    private:
        // hide the constructor and destructor, we don't ever create an instance
        // of this class, it is entirely static functions:

        PegResourceManager(void);
        ~PegResourceManager();

        static PegBitmap **mpSystemBitmapTable;
        static PEGCOLOR *mpSystemColorTable;
        static PegFont **mpSystemFontTable;

       #ifdef PEG_PRO
        static PegGradientStruct *mpGradientCache;
       #endif

        static PEGUINT mBitmapTableSize;
        static PEGUINT mColorTableSize;
        static PEGUINT mFontTableSize;

       #ifdef PEG_PRO
        static PEGUINT mGradientCacheSize;
       #endif

       #if defined(PEG_RUNTIME_RESOURCES)

        static PEGBOOL LoadStringTable(PegFile *pFile, PEGBOOL DelOldStrs);
        static PEGBOOL LoadResTheme(PegFile *pFile, PegResourceTable *pTable);
        static PEGBOOL ReadFont(PegFile *pFile, PegResourceTable *pTable);
        static PEGBOOL ReadImage(PegFile *pFile, PegResourceTable *pTable);
        static PEGBOOL ReadColors(PegFile *pFile, PegResourceTable *pTable);

        static PegResourceTable **mpLoadedResTables;
        static PEGINT mNumLoadedThemes;
        static PEGINT mLoadedStringTableEntries;
        static PEGINT mLoadedLanguages;
        static PEGCT **mLoadedLanguageNames;

       #endif

        static PegStringTablePage *mpStringTable;
        static PEGCT **mpLanguageNames;
        static PEGUINT  mNumLanguages;
        static PEGUINT  mCurrentLanguage;
};

enum PegSysBitmapIds
{
    BID_WIN_ICON = 1,
    BID_SYS_BTN,
    BID_CLOSE_BTN,
    BID_MAX_BTN,
    BID_MIN_BTN,
    BID_RESTORE_BTN,
    BID_UP_ARROW,
    BID_DOWN_ARROW,
    BID_LEFT_ARROW,
    BID_RIGHT_ARROW,
    BID_RADIO_ON,
    BID_RADIO_OFF,
    BID_RADIO_ON_DIS,
    BID_RADIO_OFF_DIS,
    BID_RADIO_ON_FOC,
    BID_RADIO_OFF_FOC,
    BID_CHECK_ON,
    BID_CHECK_OFF,
    BID_CHECK_ON_DIS,
    BID_CHECK_OFF_DIS,
    BID_CHECK_ON_FOC,
    BID_CHECK_OFF_FOC,
    BID_CHECK,
    BID_DOT,
    BID_DRAG,

    // PegFileDialog
    BID_FILESMALL,
    BID_HOME,
    BID_REFRESH,
    BID_UP_DIR,
    BID_FILTER,

    // Mouse Pointers
    BID_MOUSE,
    BID_IBEAM,
    BID_HANDPTR,
    BID_NESW_SIZE,
    BID_NWSE_SIZE,
    BID_HORZ_SIZE,
    BID_VERT_SIZE,

    // TOUCH calibration
    BID_TOUCH_CAL_TARGET,

    MAX_BIDS
};

enum PegSysColorIds
{
    CID_BLACK = 0,
    CID_RED,
    CID_GREEN,
    CID_BROWN,
    CID_BLUE,
    CID_MAGENTA,
    CID_CYAN,
    CID_LIGHTGRAY,
    CID_DARKGRAY,
    CID_LIGHTRED,
    CID_LIGHTGREEN,
    CID_YELLOW,
    CID_LIGHTBLUE,
    CID_LIGHTMAGENTA,
    CID_LIGHTCYAN,
    CID_WHITE,
    CID_HIGHLIGHT,
    CID_LOWLIGHT,
    CID_SHADOW,
    CID_ACTIVE_TITLE,   // PEG Plus only
    CID_INACTIVE_TITLE, // PEG Plus only
    CID_NORMAL_TEXT,
    CID_HIGH_TEXT,
    CID_NORM_TEXT_BACK,
    CID_HIGH_TEXT_BACK,
    CID_CLIENT,
    CID_DIALOG,
    CID_BORDER,
    CID_WIDGET_FILL,
    CID_SBUTTON_FACE,   // PEG Plus only
    CID_SBUTTON_TEXT,
    CID_CURSOR,
    CID_DESKTOP,
    CID_SCROLL_FILL,
    CID_SCROLL_THUMB,   // PEG Plus only
    CID_CHART_BACK,
    CID_CHART_GRID,
    CID_CHART_LABEL,
    CID_CHART_LINE,
    CID_DIAL_NEEDLE,
    CID_DIAL_ANCHOR,
    CID_TREE_LINE,
    CID_TREE_TOGGLE,
    CID_HELP_PRMPT,

    CID_COMBOBOX_TEXTBACK,

    CID_MENUBTN,
    CID_MENUBAR_HOV,
    CID_MENUBTN_HOV,
    CID_MENUBTN_SEP1,
    CID_MENUBTN_SEP2,
    CID_MENUBTN_DIS,

   #if defined(PEG_PRO)

    CID_BTN_BORDER1,
    CID_BTN_BORDER1_PSH,
    CID_BTN_BORDER1_FOC,
    CID_BTN_BORDER1_DIS,
    CID_BTN_BORDER2,
    CID_BTN_BORDER2_PSH,
    CID_BTN_BORDER2_FOC,
    CID_BTN_BORDER2_DIS,
    CID_BTN_GRAD1,
    CID_BTN_GRAD1_PSH,
    CID_BTN_GRAD1_FOC,
    CID_BTN_GRAD1_DIS,
    CID_BTN_GRAD2,
    CID_BTN_GRAD2_PSH,
    CID_BTN_GRAD2_FOC,
    CID_BTN_GRAD2_DIS,
    CID_TITLE_GRAD1,
    CID_TITLE_GRAD2,
    CID_TITLE_GRAD3,
    CID_TITLE_GRAD4,
    CID_TITLE_GRAD5,
    CID_TITLE_GRAD1_SEL,
    CID_TITLE_GRAD2_SEL,
    CID_TITLE_GRAD3_SEL,
    CID_TITLE_GRAD4_SEL,
    CID_TITLE_GRAD5_SEL,
    CID_MENUBAR_GRAD1,
    CID_MENUBAR_GRAD2,

    CID_SLIDEBTN_GRAD1,
    CID_SLIDEBTN_GRAD1_FOC,
    CID_SLIDEBTN_GRAD1_PSH,
    CID_SLIDEBTN_GRAD2,
    CID_SLIDEBTN_GRAD2_FOC,
    CID_SLIDEBTN_GRAD2_PSH,
   #endif

    MAX_CIDS
};

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
#define DEFAULT_FONT_PTR  &SysFont
#define SMALL_FONT_PTR    &MenuFont

#define DEF_TITLE_FONT      DEFAULT_FONT_PTR
#define DEF_MENU_FONT       SMALL_FONT_PTR
#define DEF_TBUTTON_FONT    SMALL_FONT_PTR
#define DEF_MLTBUTTON_FONT  SMALL_FONT_PTR
#define DEF_RBUTTON_FONT    SMALL_FONT_PTR
#define DEF_DBUTTON_FONT    SMALL_FONT_PTR
#define DEF_CHECKBOX_FONT   SMALL_FONT_PTR
#define DEF_PROMPT_FONT     DEFAULT_FONT_PTR
#define DEF_EDITFLD_FONT    DEFAULT_FONT_PTR
#define DEF_TEXTBOX_FONT    DEFAULT_FONT_PTR
#define DEF_GROUP_FONT      SMALL_FONT_PTR
#define DEF_ICON_FONT       SMALL_FONT_PTR
#define DEF_CELL_FONT       SMALL_FONT_PTR
#define DEF_HEADER_FONT     DEFAULT_FONT_PTR
#define DEF_TAB_FONT        SMALL_FONT_PTR
#define DEF_MESGWIN_FONT    SMALL_FONT_PTR
#define DEF_TREEVIEW_FONT   SMALL_FONT_PTR
#define DEF_PROGBAR_FONT    DEFAULT_FONT_PTR
#define DEF_STATBAR_FONT    DEFAULT_FONT_PTR


enum PEG_DEFAULT_FONT_IDS {
    FID_DEFAULT = 0,           // default = SysFont
    FID_SMALL,
    FID_TITLE,                 // default = SysFont
    FID_MENU,                  // default = MenuFont
    FID_TBUTTON,               // default = MenuFont
    FID_MLTBUTTON,
    FID_RBUTTON,               // default = MenuFont
    FID_DBUTTON,
    FID_CHECKBOX,              // default = MenuFont
    FID_PROMPT,                // default = SysFont
    FID_EDITFLD,               // default = SysFont
    FID_TEXTBOX,               //
    FID_GROUP,
    FID_ICON,
    FID_CELL,
    FID_HEADER,
    FID_TAB,
    FID_MESGWIN,
    FID_TREEVIEW,
    FID_PROGBAR,
    FID_STATBAR,
    FID_RES21,
    FID_RES22,
    FID_RES23,
    FID_RES24,
    FID_RES25,
    FID_RES26,
    FID_RES27,
    FID_RES28,
    FID_RES29,
    FID_SYSFONT,
    FID_MENUFONT,
    MAX_FIDS
};

#endif  // sentry

